package cn.edu.pet.repo;


import cn.edu.pet.db.BaseRepo;
import cn.edu.pet.db.ResultSetVisitor;
import cn.edu.pet.entity.Pets;
import cn.edu.pet.entity.User;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class PetRepo extends BaseRepo {
    private PetRepo() {

    }

    private static PetRepo instance = new PetRepo();

    public static PetRepo getInstance() {
        return instance;
    }


    public boolean insertPets(Pets pets){
        String sql = String.format("insert into pets(name,intro,type,image,gender,status) value('%s','%s','%s','%s','%s','%s')", pets.getName(), pets.getIntro(), pets.getType(), pets.getImage(), pets.getGender(),pets.getStatus());
        try {
            return this.execute(sql);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public List<Pets> selectList(String name) {
        String sql;
        if (name != null && !"".equals(name)) {
            sql = String.format("select * from pets where name = %s", name);
        } else {
            sql = "select * from pets";
        }
        List<Pets> list = new ArrayList<>();
        this.query(sql, new ResultSetVisitor() {
            @Override
            public void visit(ResultSet resultSet) throws SQLException {
                Pets pets = new Pets();
                pets.setId(resultSet.getInt(1));
                pets.setName(resultSet.getString(2));
                pets.setIntro(resultSet.getString(3));
                pets.setType(resultSet.getString(4));
                pets.setImage(resultSet.getString(5));
                pets.setGender(resultSet.getString(6));
                pets.setStatus(resultSet.getString(7));
                list.add(pets);
            }
        });
        return list;
    }

    public boolean deletePets(Integer id) {
        String sql = String.format("delete from pets where id = %s", id);
        try {
            return this.execute(sql);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public Pets selectById(Integer id) {
        String sql = String.format("select * from pets where id = %s", id);
        Pets pets = new Pets();
        this.query(sql, resultSet -> {
            try {
                pets.setId(resultSet.getInt(1));
                pets.setName(resultSet.getString(2));
                pets.setIntro(resultSet.getString(3));
                pets.setType(resultSet.getString(4));
                pets.setImage(resultSet.getString(5));
                pets.setGender(resultSet.getString(6));
                pets.setStatus(resultSet.getString(7));
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        });
        return pets;
    }

    public boolean updatePets(Pets pets) {
        String sql = String.format("update pets set name = '%s',intro = '%s',type = '%s',image = '%s',gender = '%s',status = '%s' where id = %s", pets.getName(), pets.getIntro(), pets.getType(), pets.getImage(), pets.getGender(),pets.getStatus(),pets.getId());
        try {
            return this.execute(sql);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

}
